import { request } from '@/utils/request';
import { 
  FoldersResponse, 
  FolderContentsResponse, 
  UploadResponse, 
  MultipleUploadResponse,
  ApiResponse 
} from '../types';

const API_PREFIX = '/api/oss';

export const ossApi = {
  // 获取文件夹列表
  async getFolders(): Promise<FoldersResponse> {
    const response = await request.get(`${API_PREFIX}/folders`);
    return response.data;
  },

  // 获取文件夹内容
  async getFolderContents(folderName: string): Promise<FolderContentsResponse> {
    const response = await request.post(`${API_PREFIX}/GetFoldersContents`, {
      folderName
    });
    return response.data;
  },

  // 创建文件夹
  async createFolder(folderName: string): Promise<ApiResponse> {
    const response = await request.post(`${API_PREFIX}/CreatFolders`, {
      folderName
    });
    return response.data;
  },

  // 上传单个文件
  async uploadSingleFile(file: File, folder: string): Promise<UploadResponse> {
    const formData = new FormData();
    formData.append('file', file);
    
    const response = await request.post(`${API_PREFIX}/upload/${folder}`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
    return response.data;
  },

  // 批量上传文件
  async uploadMultipleFiles(files: File[], folder: string): Promise<MultipleUploadResponse> {
    const formData = new FormData();
    files.forEach(file => {
      formData.append('files', file);
    });
    
    const response = await request.post(`${API_PREFIX}/upload-multiple/${folder}`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
    return response.data;
  },

  // 删除文件
  async deleteFile(ossPath: string): Promise<ApiResponse> {
    const response = await request.delete(`${API_PREFIX}/files`, {
      data: { ossPath }
    });
    return response.data;
  },

  // 删除文件夹
  async deleteFolder(folderName: string): Promise<ApiResponse> {
    const response = await request.delete(`${API_PREFIX}/folders/${folderName}`);
    return response.data;
  }
}; 